package com.riven.bilibilidemo.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.riven.bilibilidemo.R;
import com.riven.bilibilidemo.adapter.SectionedRecyclerViewAdapter;
import com.riven.bilibilidemo.bean.RegionDetailsHotVideoSection;
import com.riven.bilibilidemo.bean.RegionDetailsInfo;
import com.riven.bilibilidemo.bean.RegionDetailsNewsVideoSection;
import com.riven.bilibilidemo.interfaces.BiliAppService;
import com.riven.bilibilidemo.utils.HttpUtilsOne;
import com.riven.bilibilidemo.utils.LogUtil;
import com.riven.bilibilidemo.views.CircleProgressView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RegionTypeDetailsFragment extends Fragment {

    @BindView(R.id.recycle)
    RecyclerView mRecyclerView;

    @BindView(R.id.circle_progress)
    CircleProgressView mCircleProgressView;

    com.riven.bilibilidemo.views.CircleProgressView circleProgress;
    private int rid;

    private SectionedRecyclerViewAdapter mSectionedRecyclerViewAdapter;
    private String URL = "http://app.bilibili.com/";

    private List<RegionDetailsInfo.DataBean.NewBean> newsVideos = new ArrayList<>();

    private List<RegionDetailsInfo.DataBean.RecommendBean> recommendVideos = new ArrayList<>();

    public static RegionTypeDetailsFragment newInstance(int rid) {

        RegionTypeDetailsFragment fragment = new RegionTypeDetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("RegionTypeDetailsFragment", rid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_region_type_details, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        finishCreateView();
    }

    private void initRecyclerView() {
        mSectionedRecyclerViewAdapter = new SectionedRecyclerViewAdapter();
        GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 1);
        mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {

                switch (mSectionedRecyclerViewAdapter.getSectionItemViewType(position)) {
                    case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
                        return 1;

                    default:
                        return 1;
                }
            }
        });
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mSectionedRecyclerViewAdapter);
    }

    private void finishCreateView() {
        rid = getArguments().getInt("RegionTypeDetailsFragment");
        lazyLoad();
    }

    private void lazyLoad() {
        showProgressBar();
        initRecyclerView();
        loadData();
    }
    protected void showProgressBar() {

        mCircleProgressView.setVisibility(View.VISIBLE);
        mCircleProgressView.spin();
    }
    protected void loadData() {
        HttpUtilsOne.getAPIServer(BiliAppService.class,URL).getRegionDetails(rid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(regionDetailsInfo -> {

                    recommendVideos.addAll(regionDetailsInfo.getData().getRecommend());
                    newsVideos.addAll(regionDetailsInfo.getData().getNewX());
                    finishTask();
                });
    }
    protected void finishTask() {

        hideProgressBar();
        mSectionedRecyclerViewAdapter.addSection(
                new RegionDetailsHotVideoSection(getActivity(), recommendVideos));
        mSectionedRecyclerViewAdapter.addSection(
                new RegionDetailsNewsVideoSection(getActivity(), newsVideos));
        mSectionedRecyclerViewAdapter.notifyDataSetChanged();
    }
    protected void hideProgressBar() {
        mCircleProgressView.setVisibility(View.GONE);
        mCircleProgressView.stopSpinning();
    }
}
